function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["style", "thumbEnabled", "uploadRef", "tableStyle", "simpleUpload", "labelConfig"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { List, Modal as AntModal, Space } from 'antd';
import { forwardRef, useRef, useState } from 'react';
import { Layout, registerComponent, iup } from "../../../../util";
import { AttachmentProvider, useAttachment } from "../../AttachmentContext";
import { AttachmentButton, FileView, LabelSelect, RemarkModal } from "../../components";
import { AttachmentToolbar, ImageUpload } from "./components";
import "./index.less";
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var OriginFormAttachment = function OriginFormAttachment(props) {
  var _AntModal$useModal = AntModal.useModal(),
    _AntModal$useModal2 = _slicedToArray(_AntModal$useModal, 2),
    modal = _AntModal$useModal2[0],
    contextHolder = _AntModal$useModal2[1];
  var attachmentContextValues = useAttachment();
  var beforeUploadValidate = attachmentContextValues.beforeUploadValidate,
    accept = attachmentContextValues.accept,
    maxCount = attachmentContextValues.maxCount,
    disabled = attachmentContextValues.disabled,
    actions = attachmentContextValues.actions,
    permission = attachmentContextValues.permission,
    attachmentRecordList = attachmentContextValues.attachmentRecordList,
    loading = attachmentContextValues.loading,
    isReady = attachmentContextValues.isReady,
    uploading = attachmentContextValues.uploading,
    onUploadStateChange = attachmentContextValues.onUploadStateChange,
    attachmentFileList = attachmentContextValues.attachmentFileList,
    onContentChanged = attachmentContextValues.onContentChanged;
  var _props$style = props.style,
    style = _props$style === void 0 ? {} : _props$style,
    _props$thumbEnabled = props.thumbEnabled,
    thumbEnabled = _props$thumbEnabled === void 0 ? false : _props$thumbEnabled,
    _props$uploadRef = props.uploadRef,
    uploadRef = _props$uploadRef === void 0 ? useRef(null) : _props$uploadRef,
    _props$tableStyle = props.tableStyle,
    tableStyle = _props$tableStyle === void 0 ? {} : _props$tableStyle,
    labelConfig = props.labelConfig,
    _props$simpleUpload = props.simpleUpload,
    simpleUpload = _props$simpleUpload === void 0 ? false : _props$simpleUpload;
  var commonProps = {
    accept: accept,
    maxCount: maxCount,
    uploadRef: uploadRef,
    onUploaded: onContentChanged,
    onBeforeUpload: beforeUploadValidate,
    onUploadStateChange: onUploadStateChange
  };
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    curRow = _useState2[0],
    setCurRow = _useState2[1];
  var content = thumbEnabled ? /*#__PURE__*/_jsx(ImageUpload, _objectSpread(_objectSpread({}, commonProps), {}, {
    attachmentFileList: attachmentFileList,
    disabled: !isReady || disabled || (permission === null || permission === void 0 ? void 0 : permission.add) === 0 || uploading,
    onRemove: function onRemove(file) {
      var params = [file];
      actions.delete(params);
    }
  })) : /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(AttachmentToolbar, _objectSpread(_objectSpread({}, commonProps), {}, {
      disabled: disabled,
      permission: permission,
      isReady: isReady,
      simpleUpload: simpleUpload,
      uploading: uploading,
      count: attachmentRecordList.length || 0,
      zipDownload: actions.zipDownload,
      openAttachment: function openAttachment() {
        return actions.openTableAttachment({
          labelConfig: labelConfig,
          modal: modal
        });
      }
    })), attachmentFileList.length > 0 && /*#__PURE__*/_jsx(Layout.Flex, {
      style: {
        paddingTop: 12
      },
      children: /*#__PURE__*/_jsx(List, {
        size: "small",
        style: _objectSpread(_objectSpread({}, tableStyle), {}, {
          maxHeight: 250,
          overflow: 'auto'
        }),
        dataSource: attachmentFileList,
        loading: loading,
        itemLayout: "horizontal",
        renderItem: function renderItem(row) {
          var uploaded = row.status === 'done' || row.status === undefined;
          var listItemActions = [/*#__PURE__*/_jsx("a", {
            style: {
              color: 'var(--primary-color)',
              visibility: uploaded ? 'unset' : 'hidden'
            },
            onClick: function onClick() {
              if (!uploaded) return;
              setCurRow(row);
            },
            children: "\u5907\u6CE8"
          })];
          var isPreview = uploaded && actions.isPreview(row.asrName || row.fileName);
          var isFail = row.status === 'fail';
          return /*#__PURE__*/_jsxs(List.Item, {
            style: {
              padding: '8px 4px'
            },
            actions: listItemActions,
            children: [/*#__PURE__*/_jsxs("div", {
              className: "attachment-flex-center ".concat(isFail ? 'upload-failed' : ''),
              children: [/*#__PURE__*/_jsx(FileView, {
                data: {
                  fileName: row.asrName || row.fileName,
                  fileSize: row.originFileSize || row.fileSize,
                  status: row.status,
                  percent: row.percent
                },
                isPreview: isPreview,
                handlePreview: function handlePreview() {
                  return row.asrFid && actions.preview(row.asrFid);
                }
              }), /*#__PURE__*/_jsxs(Space, {
                size: 0,
                className: "attachment-space-btn",
                children: [uploaded && /*#__PURE__*/_jsxs(_Fragment, {
                  children: [/*#__PURE__*/_jsx(AttachmentButton, {
                    size: "small",
                    type: "link",
                    permissionStatus: permission === null || permission === void 0 ? void 0 : permission.preview,
                    disabled: !isPreview,
                    onClick: function onClick() {
                      return row.asrFid && actions.preview(row.asrFid);
                    },
                    children: "\u9884\u89C8"
                  }), /*#__PURE__*/_jsx(AttachmentButton, {
                    size: "small",
                    type: "link",
                    permissionStatus: permission === null || permission === void 0 ? void 0 : permission.download,
                    onClick: function onClick() {
                      return actions.download(row);
                    },
                    children: "\u4E0B\u8F7D"
                  })]
                }), /*#__PURE__*/_jsx(AttachmentButton, {
                  size: "small",
                  type: "link",
                  disabled: disabled,
                  permissionStatus: permission === null || permission === void 0 ? void 0 : permission.delete,
                  onClick: function onClick() {
                    return actions.delete([row]);
                  },
                  children: "\u5220\u9664"
                })]
              })]
            }), /*#__PURE__*/_jsxs(_Fragment, {
              children: [/*#__PURE__*/_jsx("div", {
                style: {
                  width: 150
                },
                children: row.asrFilldt
              }), /*#__PURE__*/_jsx("div", {
                style: {
                  width: 70
                },
                children: row.userName
              }), !iup.isNullOrEmpty(labelConfig) && /*#__PURE__*/_jsx(LabelSelect, {
                labelConfig: _objectSpread(_objectSpread({}, labelConfig), {}, {
                  value: row.label,
                  disabled: disabled || !uploaded
                }),
                fileId: row.asrFid,
                onChangeCallback: onContentChanged
              })]
            })]
          });
        }
      })
    })]
  });
  return /*#__PURE__*/_jsxs(Layout, {
    className: "iup-form-attachment",
    style: style,
    direction: "row",
    children: [/*#__PURE__*/_jsxs(Layout.Flex, {
      className: "iup-form-attachment-content",
      children: [content, contextHolder]
    }), /*#__PURE__*/_jsx(RemarkModal, {
      data: curRow || {},
      visible: curRow !== undefined,
      disabled: disabled,
      onCancel: function onCancel() {
        return setCurRow(undefined);
      },
      onSaved: onContentChanged
    })]
  });
};
export var FormAttachment = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var style = _ref.style,
    thumbEnabled = _ref.thumbEnabled,
    uploadRef = _ref.uploadRef,
    tableStyle = _ref.tableStyle,
    simpleUpload = _ref.simpleUpload,
    labelConfig = _ref.labelConfig,
    rest = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/_jsx(AttachmentProvider, _objectSpread(_objectSpread({}, rest), {}, {
    ref: ref,
    children: /*#__PURE__*/_jsx(OriginFormAttachment, {
      style: style,
      thumbEnabled: thumbEnabled,
      uploadRef: uploadRef,
      tableStyle: tableStyle,
      simpleUpload: simpleUpload,
      labelConfig: labelConfig
    })
  }));
});
registerComponent({
  Attachment: FormAttachment
});